package mi.payment.utils.datacollection;

import android.content.Intent;

import com.google.gson.Gson;
import com.sunmi.statistics.library.DataBean;
import com.sunmi.statistics.library.MataDataConstant;
import com.sunmi.statistics.library.StatisticsManager;

import java.util.ArrayList;
import java.util.List;

import mi.payment.BuildConfig;
import mi.payment.MyApplication;
import mi.payment.bean.datacollection.DataServiceInfo;
import mi.payment.bean.datacollection.PaySummary;
import sunmi.sunmiui.utils.LogUtil;

/**
 * Created by zjr on 2018/1/22.
 * address :zhoujirui@sunmi.com
 * 数据收集时 支付汇总工具类
 */

public class PayGatherCollection {
    private static String TAG = "PayGatherCollection";
    private static final String TYPE = "payment_app_summary_info";//业务类型：支付汇总
    private List<DataBean> list = new ArrayList<>();

    private static final String ACTION_DATA_COLLECTION = "dataservice.action.data_collection";//旧版本dataService广播
    private static final String ACTION_FLAG = "DataService";//dataService广播接收数据字段

    private static final class SingletonHolder {
        private static final PayGatherCollection INSTANCE = new PayGatherCollection();
    }

    private PayGatherCollection() {

    }

    public static PayGatherCollection getInstance() {
        return SingletonHolder.INSTANCE;
    }

    //结算完成时上送数据
    public void uploadData(PaySummary paySummary) {
        if (paySummary == null) {
            LogUtil.e(TAG, "paySummary is null");
            return;
        }

        if (BuildConfig.BUILD_TYPE.equals("futureDebug") || BuildConfig.BUILD_TYPE.equals("futureRelease")) {//新版本dataService
            sendMessage(paySummary);
        } else {//旧版本dataService
            sendBroadcast(paySummary);
        }
    }

    //连接旧dataService
    private void sendBroadcast(PaySummary paySummary) {
        Intent intent = new Intent();
        intent.setAction(ACTION_DATA_COLLECTION);
        DataServiceInfo dataServiceInfo = new DataServiceInfo(TYPE, paySummary, paySummary.getTime());
        Gson gson = new Gson();
        String jsonString = gson.toJson(dataServiceInfo);
        LogUtil.e(TAG, "jsonString = " + jsonString);
        intent.putExtra(ACTION_FLAG, jsonString);
        MyApplication.getInstance().sendBroadcast(intent);
    }

    //连接新dataService
    private void sendMessage(PaySummary paySummary) {
        long id = 1;//该id随便设置，dataService不做处理
        long time = paySummary.getTime();
        String type = MataDataConstant.TYPE_GATHER;//汇总
        String packageName = MyApplication.getInstance().getPackageName();
        String businessType = TYPE;//业务类型
        int priorityType = MataDataConstant.HIGH_PRIORITY;//高级
        int flowType = MataDataConstant.FLOW_POOR;//最低支持极省流量
        //立即上报(测试时使用定时上报，否则在dataService中无法查看到数据)
        int updateType = MataDataConstant.TYPE_IMMEDIATELY;
        Gson gson = new Gson();
        String content = gson.toJson(paySummary);//采集的数据 JSONObject
        LogUtil.e(TAG, "jsonString->content = " + content);
        DataBean dataBean = new DataBean(id, time, type, packageName, businessType, priorityType, flowType, updateType, content);
        list.add(dataBean);
        StatisticsManager.getInstance().addList(MyApplication.getInstance(), list);//上报数据
        clearData();
    }

    public void clearData() {
        list.clear();
    }

}
